
package com.john.blog.controller.back;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.john.blog.common.utils.FtpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.john.blog.common.constant.KeyConst;
import com.john.blog.common.utils.SftpUtil;
import com.john.blog.common.vo.CommonResult;
import com.john.blog.common.vo.FtpConfig;

/**
 * 图片上传
 * 
 * @author JiangGengchao
 * @date 2018年3月7日
 */
@RestController
@RequestMapping("/blog/back/upload")
public class BackUpdateController {

    private final static Logger log = LoggerFactory.getLogger(BackUpdateController.class);

    public static final String HOST = "upload";
    public static final String IMAGE_BUKECT = "front/image/";
    public static final String PARA_FILE_KEY = "editormd-image-file";

    @Value("${remote.server}")
    private String REMOTE_SERVER;
    @Value("${remote.port}")
    private Integer REMOTE_PORT;
    @Value("${remote.username}")
    private String REMOTE_USERNAME;
    @Value("${remote.password}")
    private String REMOTE_PASSWORD;
    @Value("${remote.default-path}")
    private String DEFAULT_PATH;

    @Autowired
    private HttpServletRequest request;

    /**
     * 上传图片
     */
    @RequestMapping("/uploadImage")
    public CommonResult uploadImage(@RequestParam(value = PARA_FILE_KEY, required = false) MultipartFile image) {
        return upload(image);
    }

    @RequestMapping("/uploadBlogCover")
    public CommonResult uploadBlogCover(MultipartFile image) {
        return upload(image);
    }

    private CommonResult upload(MultipartFile uploadFile) {
        if (uploadFile != null) {
            String filename = uploadFile.getOriginalFilename();
            // 上传文件到ftp
            FtpConfig ftpConfig = new FtpConfig(REMOTE_SERVER.split("//")[1], REMOTE_PORT, REMOTE_USERNAME, REMOTE_PASSWORD, DEFAULT_PATH + KeyConst.IMAGES_PATH, "utf-8");
            try {
                if (FtpUtil.uploadFile(ftpConfig, filename, uploadFile.getInputStream())) {
                    String url = REMOTE_SERVER + KeyConst.IMAGES_PATH + "/" + filename;
                    request.setAttribute("success", 1);
                    request.setAttribute("msg", "");
                    request.setAttribute("url", url);
                    return CommonResult.success(url);
                } else {
                    request.setAttribute("success", 0);
                    request.setAttribute("msg", "上传失败");
                    request.setAttribute("url", "");
                    return CommonResult.error("上传失败");
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                e.printStackTrace();
                request.setAttribute("success", 0);
                request.setAttribute("msg", e.getMessage());
                request.setAttribute("url", "");
                return CommonResult.error(e.getMessage());
            }
        } else {
            request.setAttribute("success", 0);
            request.setAttribute("msg", "上传失败,文件为空");
            request.setAttribute("url", "");
            return CommonResult.error("上传失败,文件为空");
        }
    }

}
